import { http } from '@/utils/request'
import type { ApiResponse } from '@/types'

// 测试样例接口
export interface TestCase {
  id?: number
  question_id?: number
  input: string
  expected_output: string
  is_sample?: boolean
  created_at?: string
  updated_at?: string
}

// 测试样例解析结果
export interface TestCaseParseResult {
  success: boolean
  test_cases: TestCase[]
  errors: string[]
}

export const testCaseApi = {
  // 解析测试文件
  parseTestFiles(files: File[]): Promise<ApiResponse<TestCaseParseResult>> {
    const formData = new FormData()
    files.forEach(file => {
      formData.append('files', file)
    })
    
    return http.post('/admin/testcases/parse', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 创建测试样例
  createTestCase(data: Omit<TestCase, 'id' | 'created_at' | 'updated_at'>): Promise<ApiResponse<TestCase>> {
    return http.post('/admin/testcases', data)
  },

  // 批量创建测试样例
  createTestCases(questionId: number, testCases: Omit<TestCase, 'id' | 'question_id' | 'created_at' | 'updated_at'>[]): Promise<ApiResponse<TestCase[]>> {
    return http.post(`/admin/questions/${questionId}/testcases`, { test_cases: testCases })
  },

  // 获取题目的测试样例
  getTestCases(questionId: number): Promise<ApiResponse<TestCase[]>> {
    return http.get(`/admin/questions/${questionId}/testcases`)
  },

  // 更新测试样例
  updateTestCase(id: number, data: Partial<TestCase>): Promise<ApiResponse<TestCase>> {
    return http.put(`/admin/testcases/${id}`, data)
  },

  // 删除测试样例
  deleteTestCase(id: number): Promise<ApiResponse<null>> {
    return http.delete(`/admin/testcases/${id}`)
  },

  // 批量删除测试样例
  deleteTestCases(ids: number[]): Promise<ApiResponse<null>> {
    return http.delete('/admin/testcases/batch', { data: { ids } })
  }
}
